package com.hushan.elevator.dao;

import com.hushan.elevator.model.DeviceMnt;
import com.hushan.elevator.model.MntPerson;
import com.hushan.elevator.model.User;
import jakarta.transaction.Transactional;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface MntPersonRepository  extends JpaRepository<MntPerson, Integer> {
    @Transactional
    @Modifying
    @Query(value = "update mnt_person set is_deleted = 1 where mnt_user_id = :mntUserId;",nativeQuery = true)
    void deleteById(@NotNull Integer mntUserId);

    @Query(value = """
            SELECT     p.mnt_user_id,
                p.name,
                p.dept_id,
                p.company,
                p.id_card,
                p.id_photo,
                p.tel_numb,
                p.created_time,
                p.updated_time,
                p.created_by,
                p.is_deleted FROM mnt_person p \
            INNER JOIN device_mnt_person dmp ON p.mnt_user_id = dmp.mnt_user_id \
            WHERE dmp.device_id = :deviceId and p.is_deleted = false""", nativeQuery = true)
    MntPerson findByDevice(Integer deviceId);
}
